/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.friend;

import LangTools.LangIdentifyTool;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import cn.ac.iie.di.global.GlobalParas;
import static cn.ac.iie.di.global.GlobalParas.MEDIR_TYPE;
import static cn.ac.iie.di.global.GlobalParas.pcInfo;
import cn.ac.iie.di.tools.FileTools;
import cn.ac.iie.di.tools.GenerateMD5;
import cn.ac.iie.di.tools.MMUrlPath;
//import cn.ac.iie.di.bcps.JsonBcpsSender.ClientPool;
import cn.ac.iie.di.tools.JedisConnectionHandler;
import cn.ac.iie.di.tools.TimeFormat;
import cn.ac.iie.di.tools.Tools;
import iie.mm.client.ClientAPI;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.avro.generic.GenericData;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import redis.clients.jedis.Jedis;

/**
 *
 * @author lenovo-pc
 */
public class JsonFriendTask implements Runnable {

    private final List<String> jsonList;

    static private final HashMap<String, String> JsonData;//存储Json中的数据
    static org.apache.log4j.Logger logger = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sss = new SimpleDateFormat("yyyyMMdd");

    static {
        logger = org.apache.log4j.Logger.getLogger(JsonFriendTask.class.getName());
    }

    private SimpleDateFormat formatter;

    static {

        JsonData = new HashMap<>();

    }
//    private final ClientPool pool;

    public JsonFriendTask(List<String> jsonList) {
        this.jsonList = jsonList;
//        this.pool = pool;
        this.formatter = new SimpleDateFormat("yyyy-MM");
    }

    @Override
    public void run() {
        Jedis session = null;
        try {
            session = GlobalParas.connectionHandler.getJedis();
            for (String zipJson : jsonList) {
                logger.info("the zipjson is: " + zipJson);
                String dir = zipJson.split("=")[0];
                String okfile = zipJson.split("=")[1];
                if (dir.contains("queryfriendcirclemsg")) {
                    //朋友圈查询
                    String content = readFileByAll(GlobalParas.DEC_DIR + File.separator + dir + File.separator + okfile.split(".ok")[0] + ".json");
                    JSONObject j = JSONObject.fromObject(content);
                    JSONObject j1 = JSONObject.fromObject(j.getJSONArray("subResults").get(0));
                    if (j1.getJSONArray("data").size() == 0) {
                        logger.info("the zip is: 无返回结果");
                        FileTools.deleteDir(new File(GlobalParas.DEC_DIR + File.separator + dir));
                        continue;
                    }
                    JSONObject j2 = JSONObject.fromObject(j1.getJSONArray("data").get(0));

                    //用户表消息队列字段处理
                    Map<String, Object> user = new HashMap();
                    user.put("u_icon", "");
                    switch (j2.get("strGender").toString()) {
                        case "男":
                            user.put("u_sex", 1);
                            break;
                        case "女":
                            user.put("u_sex", 0);
                            break;
                        default:;
                            user.put("u_sex", -1);
                    }
                    user.put("u_accountname", j2.get("strWxId").toString());
                    user.put("u_nickname", j2.get("strNick").toString());
                    user.put("u_idtype", "");
                    String userid = null;
                    try {
                        userid = session.get("pyq_" + j.getString("opid"));
                        if (null == userid) {
                            logger.info("redis dont have: " + "pyq_" + j.getString("opid") + " " + dir);
                            FileTools.deleteDir(new File(GlobalParas.DEC_DIR + File.separator + dir));
                            continue;
                        }
                    } catch (Exception e) {
                        logger.error("redis error: " + "pyq_" + j.getString("opid") + " " + dir);
                    }

                    user.put("u_userid", userid);
                    user.put("u_imid", j2.get("uiQQ").toString());
                    user.put("u_city", j2.get("strRegionCity").toString());
                    user.put("u_country", "");
                    user.put("u_mobilephone", j2.get("strPhone").toString());
                    user.put("u_regtime", "");
                    user.put("u_username", "");
                    user.put("u_regip", "");
                    user.put("u_address", "");
                    user.put("u_email", j2.get("strMail").toString());
                    user.put("u_province", j2.get("strRegionProvince").toString());
                    user.put("u_idno", "");
                    user.put("u_telephone", j2.get("strPhone").toString());
                    logger.info("headerKsy media start jpgPaht: " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + j2.get("strHeadPortrait_local").toString());
                    long headerStart = System.currentTimeMillis();
                    if (null == GlobalParas.pcInfo) {
                        logger.info("GlobalParas.pcInfo. is null");
                    }
                    if (!j2.get("strHeadPortrait_local").toString().trim().isEmpty()) {
                        JSONObject url = new JSONObject();
                        url.put("g_id", "-1");
                        url.put("type", j2.get("strHeadPortrait_local").toString().split("\\.")[1]);
                        url.put("table", "");
                        url.put("g_id", "");
                        url.put("u_ch_id", "");
                        url.put("m_ch_id", "");
                        url.put("m_chat_room", "");
                        url.put("q_mm_md5_rk", "");
                        if (new File(GlobalParas.DEC_DIR + File.separator + dir + File.separator + j2.get("strHeadPortrait_local").toString()).exists()) {
                            logger.info("json is: " + url.toString());
                            String headerKsy = GlobalParas.pcInfo.uploadFile(GlobalParas.DEC_DIR + File.separator + dir + File.separator + j2.get("strHeadPortrait_local").toString(), url.toString());
                            user.put("u_headerurl", headerKsy);
                            logger.info("headerKsy media return key:" + headerKsy + " time:" + (System.currentTimeMillis() - headerStart) + " ms");
                        } else {
                            logger.info("the jpg file dont exists " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + j2.get("strHeadPortrait_local").toString());
                            user.put("u_headerurl", "");
                        }
                    } else {
                        user.put("u_headerurl", "");
                    }
                    user.put("source", "pyq");
                    logger.info("CX sendUserQueue: " + user.toString());
                    GlobalParas.sendUSERQueue.put(user);

                    for (Object jj : j2.getJSONArray("arrMsg")) {
                        JSONObject arrMsg = JSONObject.fromObject(jj);

//                        List<String> q_mm_md5_fh = new ArrayList<>();
//                        List<String> q_mm_md5_rk = new ArrayList<>();
//                        List<String> q_mm_Id = new ArrayList<>();
//                        List<String> q_mm_description = new ArrayList<>();
//                        List<Integer> q_mm_type = new ArrayList<>();
//                        List<String> q_mm_title = new ArrayList<>();
//                        List<String> q_mm_url = new ArrayList<>();
//                        List<String> q_mm_urlcontent = new ArrayList<>();
                        List<String> q_point_nick = new ArrayList<>();
                        List<Long> q_point_sendtime = new ArrayList<>();

                        for (int k = 0; k < arrMsg.getJSONArray("arrPic").size(); k++) {
                            JSONObject arrPic = arrMsg.getJSONArray("arrPic").getJSONObject(k);
                            Map<String, Object> media = new HashMap<>();
                            JSONObject url1 = null;
                            if (!arrPic.getString("strUrl_local").trim().isEmpty()) {
                                logger.info("media uploadFile " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                url1 = new JSONObject();
                                url1.put("q_id", GenerateMD5.string2MD5(userid + new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")) + arrMsg.getString("strDescription")));
                                url1.put("type", arrPic.getString("strUrl_local").split("\\.")[1]);
                                url1.put("table", "t_wxq_q_mm");
                                url1.put("g_id", "");
                                url1.put("u_ch_id", "");
                                url1.put("m_ch_id", "");
                                url1.put("m_chat_room", "");
                                url1.put("q_mm_md5_rk", "");
                                if (new File(GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local")).exists()) {
                                    logger.info("json is: " + url1.toString());
                                    String picKey = GlobalParas.pcInfo.uploadFile(GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"), url1.toString());
                                    media.put("q_mm_url", picKey);
                                    media.put("q_mm_md5_rk", picKey.split("\\@")[1]);
                                    media.put("q_mm_type", GlobalParas.MEDIR_TYPE.get(arrPic.getString("strUrl_local").split("\\.")[1]));
//                                    q_mm_Id.add(picKey);
//                                    q_mm_md5_rk.add(picKey.split("\\@")[1]);
//                                    q_mm_type.add(GlobalParas.MEDIR_TYPE.get(arrPic.getString("strUrl_local").split("\\.")[1]));
                                    logger.info("media uploadFile sucess return key " + picKey);
                                } else {
                                    media.put("q_mm_url", "");
                                    media.put("q_mm_md5_rk", "");
                                    media.put("q_mm_type", -1);
//                                    q_mm_Id.add("");
//                                    q_mm_md5_rk.add("");
//                                    q_mm_type.add(-1);
                                    logger.info("the jpg file dont exists " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                }
                            } else {
                                media.put("q_mm_url", "");
                                media.put("q_mm_md5_rk", "");
                                media.put("q_mm_type", -1);
//                                q_mm_md5_rk.add("");
//                                q_mm_type.add(-1);
//                                q_mm_Id.add("");
                            }
                            media.put("q_mm_md5_fh", arrPic.getString("strMd5"));
                            media.put("q_mm_title", arrPic.getString("strTitle"));
                            media.put("q_mm_description", arrPic.getString("strDescription"));
                            media.put("q_mm_Id", arrPic.getString("strUrl_local"));
                            media.put("q_mm_urlcontent", "");
                            media.put("q_mm_inputtype", 1);
                            media.put("q_id", GenerateMD5.string2MD5(userid + new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")) + arrMsg.getString("strDescription")));
                            media.put("q_sendtime", new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")));
                            media.put("source", "pyq");
                            if (media.get("q_mm_url") == null || media.get("q_mm_url").toString().trim().isEmpty()) {
                                if (null == url1) {
                                    logger.info("media continue: xxx|" + arrPic.getString("strUrl_local") + "|xxx path: " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                } else {
                                    logger.info("media continue: qqq|" + url1.toString() + "|qqq path: " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                }
                                continue;
                            }
                            logger.info("CX sendMEDIAQueue: " + media.toString());
                            while (!GlobalParas.sendMediaQueue.offer(media)) {
                                logger.info("GlobalParas.sendMediaQueue domt put");
                                Thread.sleep(3000);
                            }
//                            q_mm_md5_fh.add(arrPic.getString("strMd5"));
//                            q_mm_title.add(arrPic.getString("strTitle"));
//                            q_mm_description.add(arrPic.getString("strDescription"));
//                            q_mm_url.add(arrPic.getString("strUrl_local"));
                        }
                        for (int k = 0; k < arrMsg.getJSONArray("arrAudio").size(); k++) {
                            JSONObject arrPic = arrMsg.getJSONArray("arrAudio").getJSONObject(k);
                            Map<String, Object> media = new HashMap<>();
                            JSONObject url1 = null;
                            if (!arrPic.getString("strUrl_local").trim().isEmpty()) {
                                logger.info("media uploadFile " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                url1 = new JSONObject();
                                url1.put("q_id", GenerateMD5.string2MD5(userid + new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")) + arrMsg.getString("strDescription")));
                                url1.put("type", arrPic.getString("strUrl_local").split("\\.")[1]);
                                url1.put("table", "t_wxq_q_mm");
                                url1.put("g_id", "");
                                url1.put("u_ch_id", "");
                                url1.put("m_ch_id", "");
                                url1.put("m_chat_room", "");
                                url1.put("q_mm_md5_rk", "");
                                if (new File(GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local")).exists()) {
                                    logger.info("json is: " + url1.toString());
                                    String picKey = GlobalParas.pcInfo.uploadFile(GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"), url1.toString());
                                    media.put("q_mm_url", picKey);
                                    media.put("q_mm_md5_rk", picKey.split("\\@")[1]);
                                    media.put("q_mm_type", GlobalParas.MEDIR_TYPE.get(arrPic.getString("strUrl_local").split("\\.")[1]));
//                                    q_mm_Id.add(picKey);
//                                    q_mm_md5_rk.add(picKey.split("\\@")[1]);
//                                    q_mm_type.add(GlobalParas.MEDIR_TYPE.get(arrPic.getString("strUrl_local").split("\\.")[1]));
                                    logger.info("media uploadFile sucess return key " + picKey);
                                } else {
                                    media.put("q_mm_url", "");
                                    media.put("q_mm_md5_rk", "");
                                    media.put("q_mm_type", -1);
//                                    q_mm_Id.add("");
//                                    q_mm_md5_rk.add("");
//                                    q_mm_type.add(-1);
                                    logger.info("the jpg file dont exists " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                }
                            } else {
                                media.put("q_mm_url", "");
                                media.put("q_mm_md5_rk", "");
                                media.put("q_mm_type", -1);
//                                q_mm_md5_rk.add("");
//                                q_mm_type.add(-1);
//                                q_mm_Id.add("");
                            }
                            media.put("q_mm_md5_fh", arrPic.getString("strMd5"));
                            media.put("q_mm_title", arrPic.getString("strTitle"));
                            media.put("q_mm_description", arrPic.getString("strDescription"));
                            media.put("q_mm_Id", arrPic.getString("strUrl_local"));
                            media.put("q_mm_urlcontent", "");
                            media.put("q_mm_inputtype", 1);
                            media.put("q_id", GenerateMD5.string2MD5(userid + new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")) + arrMsg.getString("strDescription")));
                            media.put("q_sendtime", new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")));
                            media.put("source", "pyq");
                            if (media.get("q_mm_url") == null || media.get("q_mm_url").toString().trim().isEmpty()) {
                                if (null == url1) {
                                    logger.info("media continue: xxx|" + arrPic.getString("strUrl_local") + "|xxx path: " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                } else {
                                    logger.info("media continue: qqq|" + url1.toString() + "|qqq path: " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                }
                                continue;
                            }
                            logger.info("CX sendMEDIAQueue: " + media.toString());
                            while (!GlobalParas.sendMediaQueue.offer(media)) {
                                logger.info("GlobalParas.sendMediaQueue domt put");
                                Thread.sleep(3000);
                            }
                        }
                        for (int k = 0; k < arrMsg.getJSONArray("arrVideo").size(); k++) {
                            JSONObject arrPic = arrMsg.getJSONArray("arrVideo").getJSONObject(k);
                            Map<String, Object> media = new HashMap<>();
                            JSONObject url1 = null;
                            if (!arrPic.getString("strUrl_local").trim().isEmpty()) {
                                logger.info("media uploadFile " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                url1 = new JSONObject();
                                url1.put("q_id", GenerateMD5.string2MD5(userid + new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")) + arrMsg.getString("strDescription")));
                                url1.put("type", arrPic.getString("strUrl_local").split("\\.")[1]);
                                url1.put("table", "t_wxq_q_mm");
                                url1.put("g_id", "");
                                url1.put("u_ch_id", "");
                                url1.put("m_ch_id", "");
                                url1.put("m_chat_room", "");
                                url1.put("q_mm_md5_rk", "");
                                if (new File(GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local")).exists()) {
                                    logger.info("json is: " + url1.toString());
                                    String picKey = GlobalParas.pcInfo.uploadFile(GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"), url1.toString());
                                    media.put("q_mm_url", picKey);
                                    media.put("q_mm_md5_rk", picKey.split("\\@")[1]);
                                    media.put("q_mm_type", GlobalParas.MEDIR_TYPE.get(arrPic.getString("strUrl_local").split("\\.")[1]));
//                                    q_mm_Id.add(picKey);
//                                    q_mm_md5_rk.add(picKey.split("\\@")[1]);
//                                    q_mm_type.add(GlobalParas.MEDIR_TYPE.get(arrPic.getString("strUrl_local").split("\\.")[1]));
                                    logger.info("media uploadFile sucess return key " + picKey);
                                } else {
                                    media.put("q_mm_url", "");
                                    media.put("q_mm_md5_rk", "");
                                    media.put("q_mm_type", -1);
//                                    q_mm_Id.add("");
//                                    q_mm_md5_rk.add("");
//                                    q_mm_type.add(-1);
                                    logger.info("the jpg file dont exists " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                }
                            } else {
                                media.put("q_mm_url", "");
                                media.put("q_mm_md5_rk", "");
                                media.put("q_mm_type", -1);
//                                q_mm_md5_rk.add("");
//                                q_mm_type.add(-1);
//                                q_mm_Id.add("");
                            }
                            media.put("q_mm_md5_fh", arrPic.getString("strMd5"));
                            media.put("q_mm_title", arrPic.getString("strTitle"));
                            media.put("q_mm_description", arrPic.getString("strDescription"));
                            media.put("q_mm_Id", arrPic.getString("strUrl_local"));
                            media.put("q_mm_urlcontent", "");
                            media.put("q_mm_inputtype", 1);
                            media.put("q_id", GenerateMD5.string2MD5(userid + new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")) + arrMsg.getString("strDescription")));
                            media.put("q_sendtime", new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")));
                            media.put("source", "pyq");
                            if (media.get("q_mm_url") == null || media.get("q_mm_url").toString().trim().isEmpty()) {
                                if (null == url1) {
                                    logger.info("media continue: xxx|" + arrPic.getString("strUrl_local") + "|xxx path: " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                } else {
                                    logger.info("media continue: qqq|" + url1.toString() + "|qqq path: " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + arrPic.getString("strUrl_local"));
                                }
                                continue;
                            }

                            logger.info("CX sendMEDIAQueue: " + media.toString());
                            while (!GlobalParas.sendMediaQueue.offer(media)) {
                                logger.info("GlobalParas.sendMediaQueue domt put");
                                Thread.sleep(3000);
                            }
                        }
                        for (int k = 0; k < arrMsg.getJSONArray("arrLike").size(); k++) {
                            JSONObject arrPic = arrMsg.getJSONArray("arrLike").getJSONObject(k);
                            q_point_nick.add(arrPic.getString("strNick"));
                            q_point_sendtime.add(new TimeFormat().getTimestamp(arrPic.getString("strSendTime")));
                        }
                        //朋友圈表队列
                        Map<String, Object> pyq = new HashMap();
                        pyq.put("q_contentstyle", Double.valueOf(arrMsg.getString("uiContentStyle")).intValue());
                        pyq.put("q_sendcountry", arrMsg.getString("strSendCountry"));
                        pyq.put("q_sendprovince", arrMsg.getString("strSendProvince"));
                        pyq.put("q_sendcity", arrMsg.getString("strSendCity"));
                        pyq.put("q_sendtime", new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")));
                        pyq.put("q_description", arrMsg.getString("strDescription"));
                        pyq.put("q_title", arrMsg.getString("strTitle"));
                        pyq.put("q_sendcountry", arrMsg.getString("strSendCountry"));
                        pyq.put("q_uicommentcnt", Double.valueOf(arrMsg.getString("uiCommentCnt")).intValue());
                        pyq.put("q_uilikecnt", Double.valueOf(arrMsg.getString("uiLikeCnt")).intValue());
                        pyq.put("q_userid", userid);
                        pyq.put("q_id", GenerateMD5.string2MD5(userid + new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")) + arrMsg.getString("strDescription")));
                        pyq.put("q_ip", "");
                        pyq.put("q_status", new ArrayList<Integer>());
                        pyq.put("q_statustime", new ArrayList<Integer>());
                        pyq.put("q_ip_sendcountry", "");
                        pyq.put("q_ip_sendprovince", "");
                        pyq.put("q_ip_sendcity", "");
                        pyq.put("q_point_nick", q_point_nick);
                        pyq.put("q_point_sendtime", q_point_sendtime);
                        pyq.put("source", "pyq");
                        logger.info("CX sendPYQQueue: " + pyq.toString());
                        GlobalParas.sendPYQQueue.put(pyq);

                        for (int k = 0; k < arrMsg.getJSONArray("arrComment").size(); k++) {
                            JSONObject arrPic = arrMsg.getJSONArray("arrComment").getJSONObject(k);
                            Map<String, Object> cmd = new HashMap();
                            cmd.put("q_com_nick", arrPic.getString("strNick"));
                            cmd.put("q_com_content", arrPic.getString("strContent"));
                            cmd.put("q_com_sendtime", new TimeFormat().getTimestamp(arrPic.getString("strSendTime")));
                            cmd.put("q_id", GenerateMD5.string2MD5(userid + new TimeFormat().getTimestamp(arrMsg.getString("strSendTime")) + arrMsg.getString("strDescription")));
                            cmd.put("q_com_id", GenerateMD5.string2MD5(arrPic.getString("strNick") + arrPic.getString("strContent") + arrPic.getString("strSendTime")));
                            logger.info("CX sendCMDQueue: " + cmd.toString());
                            GlobalParas.sendCMDQueue.put(cmd);
                        }

                    }
                } else if (dir.contains("addUserFriendCircleMonitor")) {
                    //添加布控
                    logger.info("the zip is addUserFriendCircleMonitor");
                    String content = readFileByAll(GlobalParas.DEC_DIR + File.separator + dir + File.separator + okfile.split(".ok")[0] + ".json");
                    JSONObject j = JSONObject.fromObject(content);
                    JSONObject j1 = JSONObject.fromObject(j.getJSONArray("subResults").get(0));
                    JSONObject j2 = JSONObject.fromObject(j1.getJSONArray("data").get(0));
                    Map<String, Object> user = new HashMap<>();
                    try {
                        user.put("u_icon", j2.getString("icon"));
                    } catch (Exception e) {
                        logger.error("u_icon dont have icon " + e, e);
                        user.put("u_icon", "");
                    }

                    user.put("u_sex", j2.getInt("sex"));
                    user.put("u_accountname", j2.getString("accountname"));
                    user.put("u_nickname", j2.getString("nickname"));
                    try {
                        user.put("u_idtype", j2.getString("idtype"));
                    } catch (Exception e) {
                        logger.error("u_idtype dont have idtype " + e, e);
                        user.put("u_idtype", "");
                    }

                    String userid = null;
                    try {
                        userid = session.get("bcp_" + j.getString("opid"));
                        if (null == userid) {
                            logger.info("redis dont have: " + "bcp_" + j.getString("opid") + " " + dir);
                            FileTools.deleteDir(new File(GlobalParas.DEC_DIR + File.separator + dir));
                            continue;
                        }
                    } catch (Exception e) {
                        logger.error("redis error: " + "bcp_" + j.getString("opid") + " " + dir);
                    }
                    user.put("u_userid", userid);
                    user.put("u_imid", j2.getString("imid"));
                    try {
                        user.put("u_city", j2.getString("city"));
                    } catch (Exception e) {
                        logger.error("u_city dont have city " + e, e);
                        user.put("u_city", "");
                    }
                    try {
                        user.put("u_country", j2.getString("country"));
                    } catch (Exception e) {
                        logger.error("u_country dont have country " + e, e);
                        user.put("u_country", "");
                    }

                    user.put("u_mobilephone", j2.getString("mobilephone"));
                    try {
                        if (j2.getString("regtime").trim().isEmpty()) {
                            user.put("u_regtime", -1);
                        } else {
                            user.put("u_regtime", new TimeFormat().getTimestamp(j2.getString("regtime")));
                        }
                    } catch (Exception e) {
                        logger.error("u_regtime dont have regtime " + e, e);
                        user.put("u_regtime", "");
                    }
                    try {
                        user.put("u_username", j2.getString("username"));
                    } catch (Exception e) {
                        logger.error("u_username dont have username " + e, e);
                        user.put("u_username", "");
                    }
                    try {
                        user.put("u_regip", j2.getString("regip"));
                    } catch (Exception e) {
                        logger.error("u_regip dont have regip " + e, e);
                        user.put("u_regip", "");
                    }
                    try {
                        user.put("u_regip", j2.getString("regip"));
                    } catch (Exception e) {
                        logger.error("u_regip dont have regip " + e, e);
                        user.put("u_regip", "");
                    }
                    try {
                        user.put("u_address", j2.getString("address"));
                    } catch (Exception e) {
                        logger.error("u_address dont have address " + e, e);
                        user.put("u_address", "");
                    }
                    try {
                        user.put("u_email", j2.getString("email"));
                    } catch (Exception e) {
                        logger.error("u_email dont have email " + e, e);
                        user.put("u_email", "");
                    }
                    try {
                        user.put("u_province", j2.getString("province"));
                    } catch (Exception e) {
                        logger.error("u_province dont have province " + e, e);
                        user.put("u_province", "");
                    }
                    try {
                        user.put("u_idno", j2.getString("idno"));
                    } catch (Exception e) {
                        logger.error("u_idno dont have idno " + e, e);
                        user.put("u_idno", "");
                    }
                    try {
                        user.put("u_telephone", j2.getString("telephone"));
                    } catch (Exception e) {
                        logger.error("u_telephone dont have telephone " + e, e);
                        user.put("u_telephone", "");
                    }

                    user.put("u_headerurl", "");
                    user.put("source", "bcp");
                    logger.info("BCP sendUserQueue: " + user.toString());
                    GlobalParas.sendUSERQueue.put(user);

                } else if (dir.contains("WXB_ICP_1030036_FRIENDCYCLE")) {
                    String[] content = readFileByLines(GlobalParas.DEC_DIR + File.separator + dir + File.separator + okfile.split(".ok")[0] + ".bcp");
                    int num = readXMLFile(GlobalParas.DEC_DIR + File.separator + dir + File.separator + "GAB_ZIP_INDEX.xml");
                    if (num == 11) {
                        //bcp 评论返回
                        for (String str : content) {
                            logger.info("the zip is FRIENDCYCLECOMMOND");
                            String[] bcp = str.split("\t");
                            System.out.println("AAA" + bcp.length);
                        }
                    } else if (num == 29) {
                        //bcp 朋友圈返回
                        for (String str : content) {
                            logger.info("the zip is FRIENDCYCLE");
                            String[] ybcp = str.split("\t");
                            String[] bcp = new String[29];
                            if (ybcp.length < 29) {
                                for (int i = 0; i < ybcp.length; i++) {
                                    bcp[i] = ybcp[i];
                                }
                                for (int i = bcp.length; i < bcp.length - ybcp.length; i++) {
                                    bcp[i] = "";
                                }
                            } else {
                                bcp = ybcp;
                            }
                            Map<String, Object> pyq = new HashMap<>();
                            pyq.put("q_contentstyle", -1);
                            if (bcp.length >= 28) {
                                pyq.put("q_sendcountry", bcp[27]);
                            } else {
                                pyq.put("q_sendcountry", "");
                            }
                            pyq.put("q_sendprovince", bcp[1]);
                            pyq.put("q_sendcity", bcp[6]);
                            pyq.put("q_sendtime", Long.valueOf(bcp[11]));
                            pyq.put("q_description", bcp[12]);
                            pyq.put("q_title", bcp[22]);
                            pyq.put("q_uicommentcnt", bcp[8]);
                            pyq.put("q_uilikecnt", bcp[16]);
                            String userid = null;
                            try {
                                userid = session.get("acc_" + bcp[13]);
                                if (null == userid) {
                                    logger.info("redis dont have: " + "acc_" + bcp[13] + " " + dir);
                                    continue;
                                }
                            } catch (Exception e) {
                                logger.error("redis error: " + "acc_" + bcp[13] + " " + dir);
                            }
                            pyq.put("q_userid", userid);
                            pyq.put("q_id", GenerateMD5.string2MD5(userid + Long.valueOf(bcp[11]) + bcp[12]));
                            pyq.put("q_ip", bcp[24]);
                            pyq.put("q_status", new ArrayList<Long>());
                            pyq.put("q_statustime", new ArrayList<Long>());
                            pyq.put("q_ip_sendcountry", "");
                            pyq.put("q_ip_sendprovince", "");
                            pyq.put("q_ip_sendcity", "");
                            List<String> q_point_nick = new ArrayList<>();
                            List<Long> q_point_sendtime = new ArrayList<>();
                            String[] pics = bcp[18].split("\\,");
                            for (String pic : pics) {
                                Map<String, Object> media = new HashMap<>();
                                media.put("q_mm_md5_fh", "");
                                media.put("q_mm_description", "");
                                media.put("q_mm_title", "");
                                media.put("q_mm_urlcontent", "");
                                media.put("q_mm_inputtype", 0);
                                media.put("q_id", GenerateMD5.string2MD5(userid + Long.valueOf(bcp[11]) + bcp[12]));
                                media.put("q_sendtime", Long.valueOf(bcp[11]));
                                media.put("source", "bcp");
                                JSONObject url1 = null;
                                if (!pic.trim().isEmpty()) {
                                    logger.info("media uploadFile " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + pic);
                                    url1 = new JSONObject();
                                    url1.put("q_id", GenerateMD5.string2MD5(userid + Long.valueOf(bcp[11]) + bcp[12]));
                                    url1.put("type", pic.split("\\.")[1]);
                                    url1.put("table", "t_wxq_q_mm");
                                    url1.put("g_id", "");
                                    url1.put("u_ch_id", "");
                                    url1.put("m_ch_id", "");
                                    url1.put("m_chat_room", "");
                                    url1.put("q_mm_md5_rk", "");
                                    logger.info("json is: " + url1.toString());
                                    if (new File(GlobalParas.DEC_DIR + File.separator + dir + File.separator + pic).exists()) {
                                        String picKey = GlobalParas.pcInfo.uploadFile(GlobalParas.DEC_DIR + File.separator + dir + File.separator + pic, url1.toString());
                                        media.put("q_mm_md5_rk", picKey.split("\\@")[1]);
                                        media.put("q_mm_Id", pic);
                                        media.put("q_mm_url", picKey);
                                        media.put("q_mm_type", GlobalParas.MEDIR_TYPE.get(pic.split("\\.")[1]));
                                        logger.info("media uploadFile sucess return key " + picKey);
                                    } else {
                                        media.put("q_mm_md5_rk", "");
                                        media.put("q_mm_Id", "");
                                        media.put("q_mm_url", "");
                                        media.put("q_mm_type", -1);
                                        logger.info("the jpg file dont exists " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + pic);
                                    }

                                } else {
                                    media.put("q_mm_md5_rk", "");
                                    media.put("q_mm_Id", "");
                                    media.put("q_mm_url", "");
                                    media.put("q_mm_type", -1);
                                }
                                if (media.get("q_mm_url") == null || media.get("q_mm_url").toString().trim().isEmpty()) {
                                    if (null == url1) {
                                        logger.info("media continue: xxx|" + pic + "|xxx path: " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + pic);
                                    } else {
                                        logger.info("media continue: qqq|" + url1.toString() + "|qqq path: " + GlobalParas.DEC_DIR + File.separator + dir + File.separator + pic);
                                    }
                                    continue;
                                }
                                logger.info("BCP sendMEDIAQueue: " + media.toString());
                                while (!GlobalParas.sendMediaQueue.offer(media)) {
                                    logger.info("GlobalParas.sendMediaQueue domt put");
                                    Thread.sleep(3000);
                                }
                            }
                            pyq.put("q_point_nick", q_point_nick);
                            pyq.put("q_point_sendtime", q_point_sendtime);
                            pyq.put("source", "bcp");
                            logger.info("BCP sendPYQQueue: " + pyq.toString());
                            GlobalParas.sendPYQQueue.put(pyq);
                        }
                    } else {
                        logger.info("bcp other size is " + num);
                    }
                }
                FileTools.deleteDir(new File(GlobalParas.DEC_DIR + File.separator + dir));
            }

        } catch (Exception ex) {
            logger.error("know Bcp Task error ", ex);
            ex.printStackTrace();
        } finally {
            if (session != null) {
                JedisConnectionHandler.returnResource(session);
            }
        }
    }

    public String[] readFileByLines(String fileName) {

        BufferedReader reader = null;
        String filetype = null;
        try {
            long begin = System.currentTimeMillis();
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束  
            StringBuffer sb = new StringBuffer();
            while ((tempString = reader.readLine()) != null) {
                sb.append(tempString + "\n");
            }
            reader.close();
            long end = System.currentTimeMillis();
            System.out.println("consume " + (end - begin) + " ms");
            return sb.toString().split("\n");
        } catch (Exception e) {
            logger.error("the error is :" + e, e);
        }
        return new String[0];
    }

    public String readFileByAll(String fileName) {

        BufferedReader reader = null;
        String filetype = null;
        try {
            long begin = System.currentTimeMillis();
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束  
            StringBuffer sb = new StringBuffer();
            while ((tempString = reader.readLine()) != null) {
                sb.append(tempString.trim());
            }
            reader.close();
            long end = System.currentTimeMillis();
            System.out.println("consume " + (end - begin) + " ms");
            return sb.toString();
        } catch (Exception e) {
            logger.error("the error is :" + e, e);
        }
        return "";
    }

    public int readXMLFile(String fileName) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document documents = reader.read(new File(fileName));
        Element root = documents.getRootElement();
        for (Element e : root.element("DATASET").element("DATA").element("DATASET").element("DATA").elements()) {
            if ("DATASET".equals(e.getName())) {
                for (Attribute a : e.attributes()) {
                    if ("BCP文件数据结构".equals(a.getText())) {
                        return e.element("DATA").elements().size();
                    }
                }
            }
        }
        return 0;
    }

}
